<?php

/**
 * Contains a class to display pages under /info/
 *
 * PHP Version 5
 *
 * @category BuyLocal
 * @package  BuyLocal
 * @author   Kevin Fodness <kevin.fodness@gmail.com>
 * @author   Mike Pennisi <mpennisi@andrew.cmu.edu>
 * @author   Aaron Ryden <aryden@gmail.com>
 * @author   Jeremy Walker <walkej@rpi.edu>
 * @author   Spencer Hakim <hakims@rpi.edu>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://code.google.com/p/buylocalopensourcerensselaer/
 */
 
/** @uses escapeTextbox To allow standards-compliant display of HTML in textareas. */
require_once 'function_escapetextbox.php';
 
/**
 * A class to display pages under /info/
 *
 * @category BuyLocal
 * @package  BuyLocal
 * @author   Kevin Fodness <kevin.fodness@gmail.com>
 * @author   Mike Pennisi <mpennisi@andrew.cmu.edu>
 * @author   Aaron Ryden <aryden@gmail.com>
 * @author   Jeremy Walker <walkej@rpi.edu>
 * @author   Spencer Hakim <hakims@rpi.edu>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://code.google.com/p/buylocalopensourcerensselaer/
 */
class PageInfo extends Page
{
    /**
     * Prints the page.
     *
     * @access public
     * @return null
     */
    public $displayEditbox;

    /**
     * State variable to track whether or not to update the content.
     *
     * @access public
     * @return null
     */
    public $updateContent;
    
    /**
     * Constructor function.  Calls the parent constructor and sets the title.
     */
    public function __construct()
    {
        parent::__construct();
        
        if (valueOf($_GET, 'action') == 'edit') {
            $this->displayEditbox = true;
        }

        if (count($_POST) > 0) {
            $this->updateContent = true;
        }
    }
   
    /**
     * Prints the page.
     *
     * @param string $pageName The name of the page to display.
     *
     * @access public
     * @return null
     */
    public function printContent($pageName)
    {
        echo $this->db->getContent('Info ' . $pageName . ' Text', 1), "\n";
    }
    
    /**
     * Prints a textbox and places the content from the database in the form.
     *
     * @param string $pageName The name of the page to edit.
     *
     * @access public
     * @return null
     */
    public function printEditForm($pageName)
    {
        $content = $this->db->getContent('Info ' . $pageName . ' Text', 0);
        
        echo ' <form method="post" action="">', "\n",
            '  <div>', "\n",
            '   <textarea id="contentText" name="content" rows="10" cols="50">',
            escapeTextBox($content), '</textarea>', "\n",
            '  </div>', "\n",
            '  <div>', "\n",
            '   <input type="submit" class="button" value="Submit" />', "\n",
            '   <input type="button" class="button" value="Cancel" onclick="',
            'window.location=\'', $_SERVER['PHP_SELF'], '\'',
            '" />', "\n",
            '  </div>', "\n",
            ' </form>', "\n";
    }
   
    /**
     * Function to display the page.
     *
     * @param string $pageName The name of the page to display.
     *
     * @access public
     * @return null
     */
    public function display($pageName)
    {
        if ($this->user->role == R_ADMIN && $this->updateContent == true) {
            if (isset($_POST['content'])) {
                $this->db->updateContent(
                    'Info ' . $pageName . ' Text', $_POST['content']
                );
                $this->displayEditbox = false;
            }
        }
        
        parent::setTitle('Info: ' . $pageName);
        parent::printHeader();
        if ($this->user->role == R_ADMIN && $this->displayEditbox != true) {
            echo ' <p><a href="?action=edit">Edit</a></p>', "\n";
            $this->printContent($pageName);
            
        } else if ($this->user->role == R_ADMIN) {
            $this->printEditForm($pageName);
            
        } else {
            $this->printContent($pageName);
        }
        parent::printFooter();
    }
}

?>